{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# First and Last Indices of an Element in a Sorted Array\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/y3b-nGw8qSc5bPwYLbcmzw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given a sorted array `A`, with possibly duplicated elements, find the indices of the first and last occurrences of a target element `x`. Return `-1` if the target is not found."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: A = [1, 3, 3, 5, 7, 8, 9, 9, 9, 15], target = 9\n",
    "Output: [6, 8]\n",
    "\n",
    "Input: A = [100, 150, 150, 153], target = 150\n",
    "Output: [1, 2]\n",
    "\n",
    "Input: A = [1, 2, 3, 4, 5, 6, 10], target = 9\n",
    "Output: [-1, -1]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def indications(array: list, target: int) -> list:\n",
    "    \n",
    "    # Check if the target exists in the array.\n",
    "    if target not in array:\n",
    "        return [-1] * 2\n",
    "    \n",
    "    else:\n",
    "        \n",
    "        # The given array was sorted,\n",
    "        # get the first index of the target.\n",
    "        start = array.index(target)\n",
    "        \n",
    "        end = start + 1\n",
    "        while end < len(array):\n",
    "            if array[end] != target:\n",
    "                return [start, end - 1]\n",
    "            else:\n",
    "                end += 1\n",
    "                pass\n",
    "            pass\n",
    "        return [start] * 2\n",
    "        pass\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 8]\n",
      "[1, 2]\n",
      "[-1, -1]\n",
      "[4, 4]\n"
     ]
    }
   ],
   "source": [
    "print(indications([1, 3, 3, 5, 7, 8, 9, 9, 9, 15], 9))\n",
    "print(indications([100, 150, 150, 153], 150))\n",
    "print(indications([1, 2, 3, 4, 5, 6, 10], 9))\n",
    "print(indications([1, 2, 3, 4, 5], 5))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
